/*
 * Hibiscus is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 3 of the License,
 * or (at your option) any later version.
 *
 * Hibiscus is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Hibiscus; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 * Copyright (C) 2003 Tetete - http://tetete.hu.
 */
package hu.tetete.hibiscus.taxontree;

import java.io.File;
import java.util.EnumMap;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import hu.tetete.hibiscus.R;
import hu.tetete.hibiscus.model.Taxon;
import hu.tetete.hibiscus.model.TaxonLevel;
import hu.tetete.hibiscus.taxondetails.TaxonDetailsTabActivity;
import hu.tetete.hibiscus.userfile.UserfileUtil;

/**
 * TODO javadoc.
 */
public class TaxonTreeExpandableListAdapter extends BaseExpandableListAdapter {

    private static final String TAG = TaxonTreeExpandableListAdapter.class.getName();

    private final Context                          context;
    private final EnumMap<TaxonLevel, List<Taxon>> structure;

    /**
     * @param context The context of the expandable list
     * @param structure TODO javadoc.
     */
    public TaxonTreeExpandableListAdapter(final Context context, final EnumMap<TaxonLevel, List<Taxon>> structure) {
        super();
        this.context   = context;
        this.structure = structure;
    }

    private TextView getGenericView() {
        // Layout parameters for the ExpandableListView
        final AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT, 64);

        final TextView textView = new TextView(this.context);
        textView.setLayoutParams(lp);
        // Center the text vertically
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        // Set the text starting position
        textView.setPadding(36, 0, 0, 0);
        return textView;
    }

    /**
     * @see android.widget.ExpandableListAdapter#getChild(int, int)
     */
    @Override
    public Object getChild(final int groupPosition, final int childPosition) {
        final Taxon taxon = this.structure.get(getGroup(groupPosition)).get(childPosition);
        Log.i(TAG, "ChildPosition: " + childPosition + " Object: " + taxon);
        return taxon;
    }

    /**
     * @see android.widget.ExpandableListAdapter#getChildId(int, int)
     */
    @Override
    public long getChildId(final int groupPosition, final int childPosition) {
        return childPosition;
    }

    /**
     * @see android.widget.ExpandableListAdapter#getChildView(int, int, boolean, android.view.View, android.view.ViewGroup)
     */
    @Override
    public View getChildView(final int groupPosition, final int childPosition,
            final boolean isLastChild, final View convertView, final ViewGroup parent) {
//        final TextView groupTextView = getGenericView();
//        groupTextView.setText(String.valueOf(getChild(groupPosition, childPosition)));
//        return groupTextView;
        final Taxon                childTaxon       = (Taxon)getChild(groupPosition, childPosition);

        final LayoutInflater       inflater         = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final RelativeLayout       view             = (RelativeLayout)inflater.inflate(R.layout.taxon_tree_element, null);
        final ImageView            taxonImage       = (ImageView)view.findViewById(R.id.taxonImage);
        final TextView             taxonNameField   = (TextView)view.findViewById(R.id.taxonName);
        final TextView             scienceNameField = (TextView)view.findViewById(R.id.scienceName);
        final Button               drillDownButton  = (Button)view.findViewById(R.id.drillDownButton);

        taxonNameField.setText(childTaxon.getName());
        scienceNameField.setText(childTaxon.getScienceName());

        final File photo = UserfileUtil.getTaxonPhoto(childTaxon);
        if (photo != null) {
            taxonImage.setImageURI(Uri.fromFile(photo));
        }

        //TODO disable the button if the taxon doesn't have child.
        drillDownButton.setOnClickListener(new DrillDownOnClickListener(childTaxon));
        drillDownButton.setFocusable(false);

        return view;

    }

    /**
     * @see android.widget.ExpandableListAdapter#getChildrenCount(int)
     */
    @Override
    public int getChildrenCount(final int groupPosition) {
        return this.structure.get(getGroup(groupPosition)).size();
    }


    /**
     * @see android.widget.ExpandableListAdapter#getGroup(int)
     */
    @Override
    public Object getGroup(final int groupPosition) {
        return this.structure.keySet().toArray(new TaxonLevel[0])[groupPosition];
    }

    /**
     * @see android.widget.ExpandableListAdapter#getGroupCount()
     */
    @Override
    public int getGroupCount() {
        return this.structure.size();
    }

    /**
     * @see android.widget.ExpandableListAdapter#getGroupId(int)
     */
    @Override
    public long getGroupId(final int groupPosition) {
        return groupPosition;
    }

    /**
     * @see android.widget.ExpandableListAdapter#getGroupView(int, boolean, android.view.View, android.view.ViewGroup)
     */
    @Override
    public View getGroupView(final int groupPosition, final boolean isExpanded, final View convertView, final ViewGroup parent) {
        final TextView groupTextView = getGenericView();
        groupTextView.setText(String.valueOf(getGroup(groupPosition)));
        return groupTextView;
    }

    /**
     * @see android.widget.ExpandableListAdapter#hasStableIds()
     */
    @Override
    public boolean hasStableIds() {
        return true;
    }

    /**
     * @see android.widget.ExpandableListAdapter#isChildSelectable(int, int)
     */
    @Override
    public boolean isChildSelectable(final int groupPosition, final int childPosition) {
        return true;
    }

    /**
     * It can be bind to an onClick event to drill down one level in the property tree.
     */
    private class DrillDownOnClickListener implements View.OnClickListener {

        private final Taxon taxon;

        public DrillDownOnClickListener(final Taxon taxon) {
            this.taxon = taxon;
        }

        @SuppressWarnings("synthetic-access")
        @Override
        public void onClick(final View view) {
            Log.i(TAG, "You selected: " + taxon);
            final Intent intent = new Intent(view.getContext(), TaxonDetailsTabActivity.class);
            intent.putExtra(TaxonDetailsTabActivity.INTENT_EXTRA.TAXON.name(), taxon);
            context.startActivity(intent);

        }
    }

}
